//
//  PersonalBlogViewModel.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/6.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit
import SDWebImage

class PersonalBlogViewModel: NSObject {

    var statuse: JSONModel?
    
    var headImgURL: NSURL? {
        
        let url = NSURL(string: statuse?.user?.profile_image_url ?? "")
    
        return url
    }
    
    var VIPImg: UIImage? {
        
        return UIImage(named: "common_icon_membership_level\(statuse?.user?.mbrank ?? 0)")
    }
    // -1 0个人认证  2,3,5 企业 220 达人
    var verifitiedImage: UIImage? {
        
        switch statuse?.user?.verified_level ?? -1 {
            
        case -1:
            return nil
        case 0:
            return UIImage(named: "avatar_vip")
        case 2, 3, 5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return nil
        }
    }
    
    var reposts_count_str: String? {
      
        return "\(statuse?.reposts_count ?? 0)"
    }
    
    var comments_count_str: String? {
        
        return "\(statuse?.comments_count ?? 0)"
    }
    
    var attitudes_count_str: String {
        
        return "\(statuse?.attitudes_count ?? 0)"
    }
    
    //<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>
    
    var scource: String? {
        
        return getScource(statuse?.source ?? "")
    }
    
    var creat_at: String? {
        
        
        return getTime(statuse?.created_at ?? "")
    }

    private func getScource(str: String) -> String {
    
        let pattern = "<a.*?>(.*?)</a>"
        
        let expression = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
        
        let result = expression.firstMatchInString(str, options: [], range: NSMakeRange(0, str.characters.count))
        
//        print(result)
     
        if result != nil {
            
            let scourceRange = result?.rangeAtIndex(1)
            
            let scourceString = (str as NSString).substringWithRange(scourceRange!)
            
//            print(scourceString)
            return scourceString
            }
        
        return ""
    }
    
    private func getTime(str: String) -> String {
        
        let formatter = NSDateFormatter()
        
        formatter.dateFormat = "EEE MMM DD HH:mm:ss Z yyyy"
        
//        formatter.locale = NSLocale(localeIdentifier: "en_US")
        
        let date = formatter.dateFromString(str)!
//        print(str)
//        print(date)
        
        let calendar = NSCalendar.currentCalendar()
        
        let unit = NSCalendarUnit(arrayLiteral: .Year, .Month, .Day, .Hour, .Minute, .Second)
        
        let time = calendar.components(unit, fromDate: date, toDate: NSDate(), options: [])
        
//        print(time)
        
        if time.year == 0 { 
            
            if time.month == 0 {
                
                if time.day == 0 {
                    
                    if time.hour == 0 {
                        
                        if time.minute == 0 {
                            
                            return "刚刚"
                            
                        } else {
                            
                            return "\(time.minute)分钟前"
                        }
                        
                    } else {
                        
                        return "\(time.hour)小时前"
                    }
                    
                } else if time.day == 1 {
                    
                    formatter.dateFormat = "HH:mm"
                    
                    return "昨天\(formatter.dateFromString(str))"
                }
                
            }else {
                
                formatter.dateFormat = "MM-DD HH:mm"
                
                return "\(formatter.dateFromString(str))"
            }
            
        } else {
            
            formatter.dateFormat = "yyyy-MM-DD HH:mm"
            
            return "\(formatter.dateFromString(str))"

        }
        
        
        return ""
    }
    
    init(model: JSONModel) {
        
        super.init()
        
        statuse = model

    }
    
}
